package com.cblz.bd.server.core.controller;

import com.cblz.bd.common.core.entity.BaMenuRule;
import com.cblz.bd.common.core.entity.system.IndexSearchParams;
import com.cblz.bd.common.core.exception.BdAdminException;
import com.cblz.bd.common.core.utils.ResultResponse;
import com.cblz.bd.redis.spring.boot.starter.service.RedisService;
import com.cblz.bd.server.core.annotation.AdminApiController;
import com.cblz.bd.server.core.annotation.Log;
import com.cblz.bd.server.core.event.MenuRulesEditEventListen;
import com.cblz.bd.server.core.service.BaMenuRuleService;
import com.cblz.bd.server.utils.CacheCleanUtil;
import io.vavr.control.Option;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * {@code @Name:} BaMenuController
 * {@code @Description: } 菜单控制层
 * {@code @Author:} by yanjieping
 * {@code @CreateDate:} 2022/11/14 - 周一
 */
@Slf4j
@RequiredArgsConstructor
@AdminApiController("menu/admin")
public class BaMenuController {

    private final BaMenuRuleService baMenuRuleService;

    private final RedisService redisService;

    private final ApplicationEventPublisher publisher;

    private final CacheCleanUtil cacheCleanUtil;

    @PostMapping("index")
    public ResultResponse list() {
        return new ResultResponse().data(Map.of("list", Option.of(baMenuRuleService.getMenuTrees(null)).getOrElseThrow(() -> new BdAdminException("菜单信息查询结果为空"))));
    }

    /**
     * 菜单下拉框
     *
     * @param paramsData
     * @return
     */
    @GetMapping("remoteSelect")
    public ResultResponse list(@RequestParam Map<String, Object> paramsData) {
        IndexSearchParams indexSearchParams = IndexSearchParams.Map2IndexSearch(paramsData);
        return new ResultResponse().data(Map.of("list", Option.of(baMenuRuleService.getMenuTrees(indexSearchParams)).getOrElseThrow(() -> new BdAdminException("菜单信息查询结果为空"))));
    }

    /**
     * @Author: kejiang
     * @Date: 2022/11/15 15:23
     * @Description: 菜单新增
     */
    @PostMapping("add")
    @Log(description = "菜单新增数据")
    public ResultResponse add(@RequestBody BaMenuRule baMenuRule) {
        // 清除菜单缓存
        cacheCleanUtil.cleanMenuCache(true, false, (String) null);
        return new ResultResponse().data(baMenuRuleService.save(baMenuRule));
    }

    /**
     * @Author: kejiang
     * @Date: 2022/11/15 15:48
     * @Description: 获取菜单数据
     */
    @GetMapping("edit")
    public ResultResponse getEdit(@RequestParam Integer id) {
        return new ResultResponse().data(baMenuRuleService.getById(id));
    }

    /**
     * @Author: kejiang
     * @Date: 2022/11/15 16:59
     * @Description: 修改菜单数据
     */
    @PostMapping("edit")
    @Log(description = "修改菜单数据")
    @Transactional(rollbackFor = Exception.class)
    public ResultResponse postEdit(@RequestBody BaMenuRule baMenuRule) {
        if (baMenuRuleService.updateById(baMenuRule)) {
            // 清除菜单缓存
            cacheCleanUtil.cleanMenuCache(true, false, (String) null);

            publisher.publishEvent(new MenuRulesEditEventListen(this));

            return new ResultResponse().data(true);
        }
        return new ResultResponse().data(null);
    }

    /**
     * @Author: kejiang
     * @Date: 2022/11/16 9:28
     * @Description:
     */
    @DeleteMapping("del")
    @Log(description = "删除菜单数据")
    @Transactional(rollbackFor = Exception.class)
    public void del(@RequestBody Map ids) {
        List<Integer> objects = (List<Integer>) ids.get("ids");
        baMenuRuleService.removeByIds(objects);

        cacheCleanUtil.cleanMenuCache(true, true, (String) null);

        publisher.publishEvent(new MenuRulesEditEventListen(this));
    }

    @PostMapping("sortable")
    public void sortable(@RequestBody Map map) {
        baMenuRuleService.sortable((Integer) map.get("id"), (Integer) map.get("targetId"));
    }
}
